﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using Model;
using BLL.BussinessClass;
namespace AlltrustDinner.Pages
{
    public partial class Default : PageBase
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RestaurantBiz biz = new RestaurantBiz();
                List<RESTAURANT> restaurantList = biz.GetRestaurantInfo();

                if (restaurantList == null)
                {
                    return;
                }
                restaurantList = restaurantList.Where(c => c.OPENTM.Split(',').Contains(((int)DateTime.Now.DayOfWeek).ToString())).ToList<RESTAURANT>();
                this.ddl_resturant.DataSource = restaurantList;
                this.ddl_resturant.DataTextField = RESTAURANT.NODE_NAME;
                this.ddl_resturant.DataValueField = RESTAURANT.NODE_NAME;
                this.ddl_resturant.DataBind();
                BindMenuList(this.ddl_resturant.SelectedValue);

            }
            SetTodayItemList();
        }

        protected void ddl_resturant_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindMenuList(this.ddl_resturant.SelectedValue);
        }

        private void BindMenuList(string restaurantName)
        {
            RestaurantBiz biz = new RestaurantBiz();
            List<MENU> menuList = biz.LoadMenuXML(restaurantName);

            this.rpt_menu_list.DataSource = menuList;
            this.rpt_menu_list.DataBind();
        }

        private void SetTodayItemList()
        {
            JavaScriptSerializer Serializer = new JavaScriptSerializer();
            Dictionary<string, Object> returnResult = new Dictionary<string, Object>();
            OrderBiz biz = new OrderBiz();
            List<ITEM> itemList = biz.LoadTodayOrderItemList();
            USER currUser = Session["CurrentUser"] as USER;

            returnResult.Add("ItemList", itemList);
            returnResult.Add("ItemCount", itemList.Count(c => c.NAME == currUser.NAME));
            this.hid_today_item_list.Value = Serializer.Serialize(returnResult);
        }
    }
}